package com.resou.reader.discovery.bookdetail;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookListBean;
import com.resou.reader.reader.utils.BitmapUtils;
import com.resou.reader.view.ExpandableTextView;

import java.util.List;

public class BookListAdapter extends BaseQuickAdapter<BookListBean.ListBookBean.BooksBean, BaseViewHolder> {
    public BookListAdapter(int layoutResId, @Nullable List<BookListBean.ListBookBean.BooksBean> data) {
        super(layoutResId, data);
    }

    public BookListAdapter(@Nullable List<BookListBean.ListBookBean.BooksBean> data) {
        super(data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BookListBean.ListBookBean.BooksBean item) {
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_RGB_565);
        Glide.with(mContext).load(item.getNovelCover() == null ? "" : item.getNovelCover()).apply(options).into((ImageView) helper.getView(R.id.ivBookPic));
        helper.setText(R.id.tvTitle, item.getNovelName() + "");
        helper.setText(R.id.tvContent, item.getNovelSummary() + "");
        helper.setText(R.id.tvAuthor,
                item.getNovelAuthor() + "·" + setState(item.getSeries()) + "·" + (int) item.getNovelWords() / 10000 +
                        "万字");
        helper.setText(R.id.tvLabel, item.getNovelCategory() + "");
        TextView tvDes = helper.getView(R.id.tv_content);
        tvDes.setText(item.getNovelRecommend() + "");
        if (tvDes.getMaxLines() > 3) {
            tvDes.setMaxLines(3);
            tvDes.setEllipsize(TextUtils.TruncateAt.END);
            helper.setVisible(R.id.v_expansion, true);
            helper.setImageResource(R.id.v_expansion, R.mipmap.ic_showall);
            RelativeLayout rlContent = helper.getView(R.id.rlContent);
            rlContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!item.isShowAll()) {
                        item.setShowAll(true);
                        tvDes.setMaxLines(Integer.MAX_VALUE);
                        helper.setImageResource(R.id.v_expansion, R.mipmap.ic_hiddenall);
                    } else {
                        item.setShowAll(false);
                        tvDes.setMaxLines(3);
                        tvDes.setEllipsize(TextUtils.TruncateAt.END);
                        helper.setImageResource(R.id.v_expansion, R.mipmap.ic_showall);
                    }
                }
            });
        } else {
            helper.setVisible(R.id.v_expansion, false);
        }

        if (item.getUserLike() == 1) {
            helper.setImageResource(R.id.iv_Praise, R.mipmap.ic_praised);
            helper.setTextColor(R.id.tvPraiseNum, mContext.getResources().getColor(R.color.theme_color));
            helper.setText(R.id.tvPraiseNum, "点赞 " + item.getNovelLikeNum());
        } else {
            helper.setImageResource(R.id.iv_Praise, R.mipmap.ic_praise);
            helper.setTextColor(R.id.tvPraiseNum, mContext.getResources().getColor(R.color.black));
            helper.setText(R.id.tvPraiseNum, "点赞 " + item.getNovelLikeNum());
        }
        if (item.getJoin() == 1) {
            helper.getView(R.id.tvAddBookList).setVisibility(View.GONE);
            helper.getView(R.id.tvAdded).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.tvAddBookList).setVisibility(View.VISIBLE);
            helper.getView(R.id.tvAdded).setVisibility(View.GONE);
        }
        helper.addOnClickListener(R.id.tvAdded);
        helper.addOnClickListener(R.id.llBookContentOnclick);
        helper.addOnClickListener(R.id.tvAddBookList);
        helper.addOnClickListener(R.id.llPraise);
    }


    /**
     * 更新点赞状态
     *
     * @param postion 下标
     * @param state   1成功，0：取消点赞
     */
    public void updataPraiseState(int postion, int state) {
        List<BookListBean.ListBookBean.BooksBean> data = (List<BookListBean.ListBookBean.BooksBean>) getData();
        int index = 0;
        while (index < data.size()) {
            if (index == postion) {
                if (state == 1) {
                    data.get(postion).setUserLike(1);
                    data.get(postion).setNovelLikeNum((data.get(postion).getNovelLikeNum() + 1));
                } else {
                    data.get(postion).setUserLike(0);
                    data.get(postion).setNovelLikeNum((data.get(postion).getNovelLikeNum() - 1));
                }
                break;
            }
            index++;
        }
        notifyDataSetChanged();
    }

    /**
     * 加入书架的状态
     *
     * @param postion 下标
     */
    public void updataAddBookListState(int postion) {
        List<BookListBean.ListBookBean.BooksBean> data = (List<BookListBean.ListBookBean.BooksBean>) getData();
        int index = 0;
        while (index < data.size()) {
            if (index == postion) {
                data.get(postion).setJoin((1));
                break;
            }
            index++;
        }
        notifyDataSetChanged();
    }


    /**
     * 获取书籍状态
     *
     * @param series
     * @return
     */
    private String setState(int series) {
        switch (series) {
            case 1:
                return "连载中";
            case 2:
                return "暂停";
            case 3:
                return "完结";
            default:
                return "";
        }
    }
}
